OPTIONS LINESIZE=87;
TITLE 'ec';

/*** July 2008 ***/

libname lib8 'C:\Documents and Settings\user1\My Documents\bigfiles\ec8';
libname libs 'C:\Documents and Settings\user1\My Documents\My Dropbox\work\research\att\formation\EC\writing\qjep\sasfiles';

libname lib 'D:\Users\baranany\Dropbox\work\research\att\formation\EC\ec678\ec';

/***STUDY 2***/
/*READ EXPLICIT*/
data eds; set lib8.explicitec1000000;run;
data eExp; set eds;

qst = QUESTION_NAME_S;
if index(QUESTIONNAIRE_NAME_S,"ec") > 0 then do;
	if QUESTION_NAME_S = "feedback" then qst = "ecfb";
	ecc = substr(QUESTIONNAIRE_NAME_S, length(QUESTIONNAIRE_NAME_S));
	if ecc="a" then ec=1; else if ecc="b" then ec=2; 
	_ecN = substr(QUESTIONNAIRE_NAME_S, 3, 1);
	ecN = input(_ecN, 4.0);
	if ecN=1 then ecN=12;
end;

if QUESTION_NAME_S not in ("feedback" "text") then	expR = INPUT(QUESTION_RESPONSE_S, 4.0); 

subject=SESSION_ID; 
qstN = QUESTION_NUMBER;
keep subject TASK_NUMBER SESSION_ID qst qstN expR ec ecc _ecN ecN; run;

proc freq data=eExp; tables qst; run;
proc freq data=eExp; tables ec; run;
proc sort data=eExp;by subject;run;
PROC TRANSPOSE DATA=eExp OUT=qds;
VAR expR;BY subject;ID qst;RUN;
data qds; set qds; drop _NAME_;run;
/*proc print data=qds;run;*/
proc contents data=qds;run;

/*
m_ecbulbasaur_  m_eccharmander_  m_ecdiglett_  m_ecjigglypuff_ m_eckakuna_ m_ecruriri_
*/
data qds; set qds;
djigrur = ecjigglypuff-ecruriri;
mecbulbasaur=m_ecbulbasaur_; meccharmander=m_eccharmander_;mecdiglett=m_ecdiglett_;
mecjigglypuff= m_ecjigglypuff_; meckakuna=m_eckakuna_;mecruriri=m_ecruriri_;
mdjigrur = mecjigglypuff-mecruriri;
certainty = mean(abs(mecjigglypuff), abs(mecruriri));
certaintyF = mean(abs(mecbulbasaur), abs(meccharmander), abs(meckakuna));
drop m_ecbulbasaur_  m_eccharmander_  m_ecdiglett_  m_ecjigglypuff_ m_eckakuna_ m_ecruriri_;
run;
PROC MEANS DATA=qds n MEAN MAXDEC=2 fw=7 std stderr;VAR djigrur mdjigrur certainty certaintyF;RUN;
/*proc contents data=qds;run;*/
data lib8.qds; set qds; run;

data ecfb; set eds; 
if index(QUESTIONNAIRE_NAME_S,"ec") > 0 and QUESTION_NAME_S = "feedback";
nCorrects = substr(QUESTION_RESPONSE_S,9,2);
nCorrect = input(nCorrects, 4.0);
nFalses = substr(QUESTION_RESPONSE_S,index(QUESTION_RESPONSE_S, "s,") + 2,3);
nFalse=  input(nFalses, 4.0);
subject=session_id;
keep nCorrect nFalse subject session_id;
run;
proc freq data=ecfb; tables nCorrect; run;
data lib8.ecfb; set ecfb; run;

*save EC condition;
PROC MEANS noprint DATA=eExp n MEAN MAXDEC=2 fw=7 std stderr; VAR ec; CLASS subject; output out=cnds mean=cond; RUN;
data cnds; set cnds; keep subject cond; run;
/*proc print data=cnds;run;*/
proc freq data=cnds; tables cond; run;
data lib8.cnds; set cnds; run;

*save EC N pairing;
PROC MEANS noprint DATA=eExp n MEAN MAXDEC=2 fw=7 std stderr; VAR ecN; CLASS subject; output out=ecN mean=ecN; RUN;
data ecn; set ecn; keep subject ecn; run;
proc freq data=ecn; tables ecn; run;
data lib8.ecn; set ecn; run;

*****************************;
*add data from registration;
data demo; set lib8.demoec; run;
proc sort data=demo; by USER_ID;run;
proc transpose data=demo name=name out=demoDS; by USER_ID; var VALUE_S;id CHARACTERISTIC_S;
data demoDS; set demoDS; 
format dPlt 2.;
dPlt = politicalid;
age = 2007-input(birthyear,4.0);
orgn = citizenship;
race='.....';
if raceomb=1 then race='ntvAm';
if raceomb=2 then race='eAsia';
if raceomb=3 then race='sAsia';
if raceomb=4 then race='natPc';
if raceomb=5 then race='black';
if raceomb=6 then race='white';
if raceomb=7 then race='blkwh';
if raceomb=8 then race='mixed';
if raceomb=9 then race='unkno';
sex = substr(sex, 1,1);
sx = '.';
sx = sex;
keep dPlt sex sx age orgn race USER_ID;run;
*merge with session data;
data ssds; set lib8.sessiontaskec1000000; run;
PROC SORT DATA=ssds; BY SESSION_ID; RUN;
PROC TRANSPOSE DATA=ssds OUT=tss PREFIX=s;VAR USER_ID;BY SESSION_ID; RUN;
data tss; set tss; USER_ID = s1; keep SESSION_ID USER_ID; run;
PROC SORT DATA=demoDS; BY USER_ID; RUN;
PROC SORT DATA=tss; BY USER_ID; RUN;
data demoDS; merge demoDS tss; by USER_ID; drop _NAME_; run;
data demoDS; set demoDS; where SESSION_ID > 0; subject = SESSION_ID; run;
data lib8.demoDS; set demoDS;run;

*****************;
data ssds; set ssds; subject = SESSION_ID;run;

/*Read duration*/
data startT; set ssds; if TASK_ID_S = "con"; startT = TASK_CREATION_DATE; run;
data endT; set ssds; if TASK_ID_S = "dbrf"; endT = TASK_CREATION_DATE; run;
PROC SORT DATA=startT; BY SESSION_ID; RUN;
PROC SORT DATA=endT; BY SESSION_ID; RUN;
data timeds; merge startT endT; by SESSION_ID; duration = endT - startT;run;
proc freq data=timeds; tables duration; run;
data lib8.timeds; set timeds;run;

/*read order*/
PROC SORT DATA=ssds; BY subject TASK_ID_S;RUN;
PROC UNIVARIATE DATA=ssds NOPRINT;
VAR TASK_NUMBER;OUTPUT OUT=usession mean=mNum mode=tNum;BY subject TASK_ID_S;RUN;
PROC TRANSPOSE DATA=usession OUT=orderDS prefix=ord;VAR TASK_ID_S;ID tNum;BY subject;RUN;
proc freq data=orderDS; tables ord3; run;
PROC SORT DATA=orderds; BY ord3; RUN;
data lib8.orderDS; set orderDS; run;

/*read ec trials*/
data ds; set lib8.iatec1000000; run;
data ds; set ds; subject=session_id;run;
proc freq data=ds; tables TASK_NAME_S; run;
proc freq data=ds; tables TRIAL_NAME_S; run;

PROC SORT DATA=ds; BY subject; RUN;PROC SORT DATA=ecn; BY subject; RUN;
data checkEC; merge ds ecn; by subject;run;

data checkEC; set checkEC;
char='F';
if index(trial_name_s, "jigg") > 0 then char='j';
else if index(trial_name_s, "rur") > 0 then char='r';
else if index(trial_name_s, "dig") > 0 then char='d';
cs=0; target=0;
if char in ('j' 'r') then CS=1;
else if char='d' then target=1;

if char='F' then filler=1; else filler=0;

nat = 'X';
if index(trial_name_s, "nat") > 0 then val='t';

run;

*for filler;
PROC MEANS DATA=checkEC noprint n MEAN MAXDEC=2 fw=7 std stderr; VAR filler; CLASS ecn subject; output out=countF; RUN;
/*proc print data=countF;run;*/
PROC SORT DATA=countF; BY subject; RUN;
PROC TRANSPOSE DATA=countF OUT=tcountF; Id _stat_; VAR filler; BY subject; copy ecn; where _TYPE_ = 3; RUN;
data tcountf; set tcountf; if _NAME_="filler"; ftrials = mean*n; run;
*for cs;
PROC MEANS DATA=checkEC noprint n MEAN MAXDEC=2 fw=7 std stderr; VAR cs; CLASS ecn subject; output out=countc; RUN;
PROC SORT DATA=countc; BY subject; RUN;
PROC TRANSPOSE DATA=countc OUT=tcountc; Id _stat_; VAR cs; BY subject; copy ecn; where _TYPE_ = 3; RUN;
data tcountc; set tcountc; if _NAME_="cs"; cstrials = mean*n; run;
*for target;
PROC MEANS DATA=checkEC noprint n MEAN MAXDEC=2 fw=7 std stderr; VAR target; CLASS ecn subject; output out=countt; RUN;
PROC SORT DATA=countt; BY subject; RUN;
PROC TRANSPOSE DATA=countt OUT=tcountt; Id _stat_; VAR target; BY subject; copy ecn; where _TYPE_ = 3; RUN;
data tcountt; set tcountt; if _NAME_="target"; ttrials = mean*n; run;

/*proc print data=tcountF;run;*/
PROC MEANS DATA=tcountf n MEAN MAXDEC=2 fw=7 std stderr;VAR mean n ftrials;CLASS ecn;RUN;
PROC MEANS DATA=tcountt n MEAN MAXDEC=2 fw=7 std stderr;VAR mean n ttrials;CLASS ecn;RUN;
PROC MEANS DATA=tcountc n MEAN MAXDEC=2 fw=7 std stderr;VAR mean n cstrials;CLASS ecn;RUN;

data tcountf; set tcountf; fillerM = mean; keep subject ecn fillerM ftrials; run;
data tcountt; set tcountt; trgtM = mean; keep subject ecn trgtM ttrials; run;
data tcountc; set tcountc; csM = mean; ntrials = n; keep subject ecn csM cstrials ntrials; run;

PROC SORT DATA=tcountf; BY subject; RUN;PROC SORT DATA=tcountt; BY subject; RUN;
PROC SORT DATA=tcountc; BY subject; RUN;
data cntec; merge tcountf tcountt tcountc; by subject; run;
PROC MEANS DATA=cntec n MEAN MAXDEC=2 fw=7 std stderr;VAR ntrials fillerM trgtM csM;CLASS ecn;RUN;
data lib8.cntec; set cntec; run;

/*PROC SORT DATA=checkec; BY ecn; RUN;
proc freq data=checkec; tables trial_name_s; by ecn; 
where index(trial_name_s, "ruri") > 0 or index(trial_name_s, "jigg") > 0; run;*/

*If needed: read all data from lib;
data qds; set lib8.qds; run;
data cnds; set lib8.cnds; run;
data ecn; set lib8.ecn; run;
data demoDS; set lib8.demoDS; run;
data ecfb; set lib8.ecfb; run;
data timeds; set lib8.timeds;run;
data cntec; set lib8.cntec; run;
data orderds; set lib8.orderds;run;
*merge with all data;
PROC SORT DATA=qds; BY subject; RUN;
PROC SORT DATA=cnds; BY subject; RUN;
PROC SORT DATA=ecn; BY subject; RUN;
PROC SORT DATA=demoDS; BY subject; RUN;
PROC SORT DATA=ecfb; BY subject; RUN;
PROC SORT DATA=timeds; BY subject; RUN;
PROC SORT DATA=cntec; BY subject; RUN;
PROC SORT DATA=orderds; BY subject; RUN;

data allds; run;
data allds; merge ecn qds cnds demoDS ecfb timeds cntec orderds; by subject; run;

data allds2; set allds; 
if cond=1 then pos = "jig"; else if cond=2 then pos = "rur";
aprf = abs(djigrur);aatt = mean(abs(ecruriri-4), abs(ecjigglypuff-4));
if mecbulbasaur < 0 then mecbulbasaur = mecbulbasaur + .5; 
else if mecbulbasaur > 0 then mecbulbasaur = mecbulbasaur - .5;
if mecharmander < 0 then mecharmander = mecharmander + .5; 
else if mecharmander > 0 then mecharmander = mecharmander - .5;
if meckakuna < 0 then meckakuna = meckakuna + .5; 
else if meckakuna > 0 then meckakuna = meckakuna - .5;
if ncorrect > 14 and nFalse < 7 and ttrials=22 and djigrur not in (.) then ok =1; else ok=0;
if pos = "jig" then dposneg = djigrur; else if pos = "rur" then dposneg = djigrur*-1; 
if mecruriri < 0 then mecruriri = mecruriri + .5; 
else if mecruriri > 0 then mecruriri = mecruriri - .5;
if mecjigglypuff < 0 then mecjigglypuff = mecjigglypuff + .5; 
else if mecjigglypuff > 0 then mecjigglypuff = mecjigglypuff - .5;

mjigrur = mecjigglypuff-mecruriri;

if pos = "jig" then do
	ECpos = ecjigglypuff; ECneg = ecruriri;
	mpos = mecjigglypuff; mneg=mecruriri*-1;
end;
else if pos = "rur" then do
	ECneg = ecjigglypuff; ECpos = ecruriri;
	mpos = mecjigglypuff*-1; mneg=mecruriri;
end;
new_macc = mean(mpos, mneg);

memused=mdjigrur*used;memdemand=mdjigrur*demand;
new_maccused = new_macc*used; 
new_maccdm = new_macc*demand; 
mpnused=new_macc*used;mpndemand=new_macc*demand; 
new_maccXusedXecn = new_macc*used*ecn;usedXecn =used*ecn;
pairedXecn = paired*ecn;
new_maccXecn = new_macc*ecn;

ebulbasaur = ecbulbasaur-4; 
echarmander=eccharmander-4;
ekakuna=eckakuna-4;

if (mecruriri ne .) and (mecjigglypuff ne .) and (djigrur ne .) and (used ne .) and (demand ne .) and (paired ne .) then rspall=1;
if ok=1 and rspall=1 then include=1;

run;

/*basics*/
proc freq data=allds2; tables sex; where include=1; run;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev lclm uclm;VAR age;where include=1; run;
/*ec*/
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg;where include=1;RUN;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; class ecn; where include=1; RUN;
PROC reg DATA=allds2;MODEL dposneg = ecn / stb;where include=1;run;
/*awareness*/
proc freq data=allds2; tables paired / missing; where include=1;run;
PROC reg DATA=allds2;MODEL dposneg = paired / stb;where include=1;run;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; class PAIRED; where include=1; RUN;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; where include=1 and paired<=1; RUN;
/*memory*/
proc freq data=allds2; tables new_macc / missing; where include=1;run;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR new_macc; class ecn; where include=1; RUN;
PROC reg DATA=allds2;MODEL new_macc = ecn / stb;where include=1;run;
PROC reg DATA=allds2;MODEL dposneg = ecn / stb;where include=1 and new_macc>0;run;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; class ecn; where include=1 and new_macc>0; RUN;
proc corr data=allds2;  var dposneg; with new_macc; where include=1; run;
/*reported used*/
proc freq data=allds2; tables used; run;
PROC reg DATA=allds2;MODEL dposneg = new_macc used new_maccused / stb;where include=1;run;
PROC reg DATA=allds2;MODEL dposneg = new_macc used new_maccused new_maccXusedXecn / stb;where include=1;run;
PROC SORT DATA=allds2; BY ecn; RUN;
PROC reg DATA=allds2;MODEL dposneg = new_macc used new_maccused / stb;where include=1; by ecn; run;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; where include=1 and ecn=12 and used<0; RUN;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; where include=1 and ecn=12 and used>0; RUN;
/*demand*/
proc freq data=allds2; tables demand; where include=1; run;
proc corr data=allds2;  var dposneg; with new_macc; where include=1 and demand<0; run;
proc corr data=allds2;  var dposneg; with new_macc; where include=1 and demand>0; run;
PROC reg DATA=allds2;MODEL dposneg = new_macc demand new_maccdm / stb;where include=1; run;
PROC reg DATA=allds2;MODEL dposneg = new_macc demand new_maccdm / stb;where include=1 and ecn>4; run;
/*used without demand*/
PROC reg DATA=allds2;MODEL dposneg = used / stb;where include=1 and demand=-3;run;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; where include=1 and demand=-3 and used<0; RUN;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; where demand=-3 and used>=0; RUN;
/*false memory*/
proc corr data=allds2;
  var mecbulbasaur meccharmander meckakuna mecruriri mecjigglypuff;
  with ecbulbasaur eccharmander eckakuna ecruriri ecjigglypuff;
  where include=1;run;
proc corr data=allds2;
  var exbulbasaur excharmander exkakuna exjigglypuff exruriri;
  with cbulbasaur ccharmander ckakuna cjigglypuff cruriri;
  where include=1;run;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;
VAR mecbulbasaur meccharmander meckakuna; where include=1; RUN;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;
VAR ecruriri ecjigglypuff mecruriri mecjigglypuff; where include=1; RUN;
/*reverse */
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR new_macc; where include=1 and dposneg<=0; RUN;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR new_macc; where include=1 and dposneg<0 and ecn=12; RUN;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR new_macc; where include=1 and dposneg<0 and ecn=4; RUN;
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR new_macc; where include=1 and dposneg<0 and ecn=8; RUN;
/*reverse EC*/
PROC MEANS DATA=allds2 n MEAN MAXDEC=2 fw=7 stderr stddev t prt;VAR dposneg; where include=1 and new_macc<=0; RUN;

